from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser


# Create your models here.
class User(AbstractUser):

    """用户模型类"""
    SEX_CHOICES = (
    (1, "男"),
    (2, "女"),
    )
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号',null=True)
    nickname = models.CharField(max_length=100, null=True, verbose_name='昵称')
    realname = models.CharField(max_length=100, null=True, verbose_name='真实名称')
    avatar = models.CharField(null=True, max_length=500, verbose_name="用户头像")
    sex = models.SmallIntegerField(choices=SEX_CHOICES, default=1,verbose_name="性别")
    birthday = models.DateField(null=True, verbose_name="出生年月日")
    website = models.CharField(max_length=100, null=True, verbose_name='个人网站')
    city = models.CharField(max_length=100, null=True, verbose_name='现居城市')
    address = models.CharField(max_length=100, null=True, verbose_name='联系住址')
    update_date = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    # 我的粉丝有谁
    fans = models.ManyToManyField(to='self', symmetrical=False,related_name='idols')
    # 我的偶像有谁
    class Meta:
        db_table = 'tb_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

# class OAuthWeixinUser(models.Model):
#     """
#     QQ登录用户数据
#     """
#     user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
#     openid = models.CharField(max_length=64, verbose_name='openid',db_index=True)
#
#     class Meta:
#         db_table = 'tb_oauth_weixin'
#         verbose_name = '微信登录用户数据'
#         verbose_name_plural = verbose_name

from datetime import datetime
# 招聘模块

# 吐槽和吐槽的评论数据都存储在mongodb中,不是存储在mysql中
# 吐槽和吐槽的评论都属于吐槽的这张表
# 吐槽的parent_id为None,评论则有parent_id
# class Spit(mongoengine.Document):
#     content = mongoengine.StringField() # 吐槽内容
#     publishtime = mongoengine.DateTimeField(default=datetime.utcnow) # 发布日期
#     userid = mongoengine.StringField() # 发布人ID
#     nickname = mongoengine.StringField() # 发布人昵称
#     visits = mongoengine.IntField(default=0) # 浏览量
#     thumbup = mongoengine.IntField(default=0) # 点赞数
#     comment = mongoengine.IntField(default=0) # 回复数
#     avatar = mongoengine.StringField() # 用户的头像
#     parent = mongoengine.ReferenceField("Spit") # 上级ID
#     collected = mongoengine.BooleanField(default=False) # 是否收藏
#     hasthumbup = mongoengine.BooleanField(default=False) # 是否点赞
#
#     meta = {'collection': 'spit'}
#
#     def __unicode__(self):
#         return self.content
"""吐槽"""
class Spit(models.Model):

    content = models.CharField(max_length=1000, null=True) # 吐槽内容
    publishtime = models.DateTimeField(auto_now_add=True) # 发布日期
    userid = models.CharField(max_length=100, null=True) # 发布人ID
    nickname = models.CharField(max_length=100, null=True) # 发布人昵称
    visits = models.IntegerField(default=0, null=True) # 浏览数
    thumbup = models.IntegerField(default=0, null=True) # 点赞数
    comment = models.IntegerField(default=0, null=True) # 回复数
    avatar = models.CharField(null=True, max_length=500) # 用户的头像
    parent = models.ForeignKey('self',
                               on_delete=models.CASCADE,
                               related_name='cms',
                               null=True,
                               blank=True,
                               verbose_name='吐槽')
    collected = models.BooleanField(default=False, null=True) # 是否收藏
    hasthumbup = models.BooleanField(default=False, null=True) # 是否点赞

    class Meta:
        db_table = 'tb_spit'
        verbose_name = '吐槽'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.content


from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models

"""活动"""

class Gathering(models.Model):

    STATE_CHOICES = (
        (0, "不可见"),
        (1, "可见"),
    )
    name = models.CharField(max_length=100, null=True, default=None,
                            verbose_name="活动名称")
    summary = models.TextField(null=True, default=None, verbose_name="活动简介")
    detail = RichTextUploadingField(default='', verbose_name='详细介绍')
    address = models.CharField(max_length=100, null=True, default=None,
                               verbose_name="举办地点")
    sponsor = models.CharField(max_length=100, null=True, default=None,
                               verbose_name="主办方")

    image = models.CharField(null=True, max_length=100, default=None,
                             verbose_name="活动封面")
    city = models.CharField(max_length=100, null=True, default=None,
                            verbose_name="举办城市")
    state = models.SmallIntegerField(choices=STATE_CHOICES, default=1,
                                     verbose_name="是否可见")
    starttime = models.DateTimeField(null=True, verbose_name="开始时间")
    endtime = models.DateTimeField(null=True, verbose_name="截止日期")
    endrolltime = models.DateTimeField(null=True, verbose_name="报名截止日期")
    users = models.ManyToManyField(User, symmetrical=False,
                                   related_name='gathers', verbose_name="参加者")

    class Meta:
        db_table = "tb_gathering"
        verbose_name = "活动"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

"""问答模块"""
from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models

"""标签"""
class Label(models.Model):

    label_name = models.CharField(max_length=20, null=True, default=None,
                                  verbose_name="标签名称")
    desc = models.CharField(max_length=200, null=True, default=None,
                            verbose_name="标签描述")
    baike_url = models.CharField(max_length=200, null=True, default=None,
                                 verbose_name="标签百科链接")
    label_icon = models.ImageField(null=True, default=None, verbose_name="标签图片")
    # 标签的关注者
    users = models.ManyToManyField(User, symmetrical=False,
                                   related_name='labels', verbose_name="关注者")

    class Meta:
        db_table = "tb_label"
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.label_name

"""问题"""
class Question(models.Model):

    title = models.CharField(max_length=100, null=True, default=None,
                             verbose_name="标题")
    content = RichTextUploadingField(default='', verbose_name='问题内容')
    visits = models.BigIntegerField(null=True, default=0, verbose_name="浏览量")
    thumbup = models.BigIntegerField(null=True, default=0, verbose_name="点赞数")
    reply = models.BigIntegerField(null=True, default=0, verbose_name="回复数")
    useful_count = models.BigIntegerField(null=True, default=0, verbose_name="有用数")
    unuseful_count = models.BigIntegerField(null=True, default=0,
                                            verbose_name="无用数")
    solve = models.CharField(max_length=1, null=True, default=None,
                             verbose_name="是否解决")
    createtime = models.DateTimeField(auto_now_add=True, null=True,
                                      verbose_name="创建日期")
    updatetime = models.DateTimeField(auto_now=True, null=True, verbose_name="修改日期")
    replyname = models.CharField(max_length=100, null=True, default=None,
                                 verbose_name="回复人昵称")
    replytime = models.DateTimeField(null=True, default=None, verbose_name="回复日期")
    user = models.ForeignKey(User, on_delete=models.CASCADE,
                             related_name="questions", default=None, verbose_name="用户ID")
    labels = models.ManyToManyField(Label, symmetrical=False,
                                    related_name='questions')  # 这个问题属于哪些标签的

    class Meta:
        db_table = "tb_question"
        verbose_name = "问题"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

"""回答"""
class Reply(models.Model):

    content = RichTextUploadingField(default='', verbose_name='回答内容')
    createtime = models.DateTimeField(auto_now_add=True, null=True,
                                      verbose_name="创建日期")
    updatetime = models.DateTimeField(auto_now=True, null=True, verbose_name="修改日期")
    useful_count = models.BigIntegerField(null=True, default=0, verbose_name="有用数")
    unuseful_count = models.BigIntegerField(null=True, default=0,
                                            verbose_name="无用数")
    parent = models.ForeignKey('self', on_delete=models.SET_NULL,
                               related_name='subs', null=True, blank=True, verbose_name='父评论')
    problem = models.ForeignKey(Question, on_delete=models.CASCADE,
                                related_name="replies", default=None, verbose_name="问题ID")
    user = models.ForeignKey(User, on_delete=models.CASCADE,
                             related_name="replies", default=None, verbose_name="用户ID")
    type = models.IntegerField(default=0, verbose_name='回答类型')  # 0:评论问题 1: 评论回答 2: 回答

    class Meta:
        db_table = "qa_reply"
        verbose_name = "回答"
        verbose_name_plural = verbose_name
        ordering = ['-createtime']  # 指明默认排序

    def __str__(self):
        return self.content


from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models
# from ask.models import Label
# from users.models import User

"""头条模块"""
"""文章"""
class Article(models.Model):

    title = models.CharField(max_length=100, null=True, default=None,
                             verbose_name="标题")
    content = RichTextUploadingField(default='', verbose_name='文章内容')
    image = models.CharField(null=True, max_length=100, default=None,
                             verbose_name="文章封面")
    createtime = models.DateTimeField(auto_now_add=True,
                                      null=True, verbose_name="创建日期")
    updatetime = models.DateTimeField(auto_now=True, null=True, verbose_name="修改日期")
    visits = models.IntegerField(null=True, default=0, verbose_name="浏览量")
    thumbup = models.IntegerField(null=True, default=0, verbose_name="点赞数")
    comment_count = models.IntegerField(null=True, default=0, verbose_name="评论数")
    user = models.ForeignKey(User, on_delete=models.CASCADE,
                             related_name='articles', verbose_name='用户ID')
    channel = models.ForeignKey("Channel", related_name="articles",
                                on_delete=models.CASCADE, null=True, default=None, verbose_name="所属频道")
    labels = models.ManyToManyField(Label, symmetrical=False,
                                    related_name='articles')  # 这个问题属于哪些标签的
    collected_users = models.ManyToManyField(User, symmetrical=False,
                                             related_name='collected_articles')  # 被哪些用户收藏

    class Meta:
        db_table = "tb_article"
        verbose_name = "文章"
        verbose_name_plural = verbose_name
        ordering = ['-createtime']

    def __str__(self):
        return self.title

"""频道"""
class Channel(models.Model):

    name = models.CharField(max_length=100, null=True, default=None,
                            verbose_name="频道名称")

    class Meta:
        db_table = "tb_channel"
        verbose_name = "频道"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

"""评论"""
class Comment(models.Model):

    content = models.CharField(max_length=1000, default='', verbose_name='回答内容')
    createtime = models.DateTimeField(auto_now_add=True, null=True,
                                      verbose_name="创建日期")
    updatetime = models.DateTimeField(auto_now=True, null=True, verbose_name="修改日期")
    parent = models.ForeignKey('self', on_delete=models.SET_NULL,
                               related_name='subs', null=True, blank=True, verbose_name='父评论')
    article = models.ForeignKey(Article, on_delete=models.CASCADE,
                                related_name="comments", default=None, verbose_name="问题ID")
    user = models.ForeignKey(User, on_delete=models.CASCADE,
                             related_name="comments", default=None, verbose_name="用户ID")

    class Meta:
        db_table = "article_comment"
        verbose_name = "评论"
        verbose_name_plural = verbose_name
        ordering = ['-createtime']  # 指明默认排序

    def __str__(self):
        return self.content


from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models


"""招聘模块"""
from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models

"""企业"""
class Enterprise(models.Model):

    name = models.CharField(max_length=100, null=True, default=None, verbose_name="企业名称")
    summary = models.CharField(max_length=1000, null=True, default=None, verbose_name="企业简介")
    content = models.CharField(max_length=1000, null=True, default=None, verbose_name="企业详细信息")
    city = models.CharField(max_length=100, null=True, default=None, verbose_name="企业所在城市")
    address = models.CharField(max_length=100, null=True, default=None,
                               verbose_name="企业地址")
    labels = models.CharField(max_length=100, null=True, default=None,
                              verbose_name="标签列表", help_text="多个标签以空格隔开")
    coordinate = models.CharField(max_length=100, null=True, default=None,
                                  verbose_name="企业坐标")
    logo = models.ImageField(null=True, default=None, verbose_name="Logo")
    url = models.CharField(max_length=100, null=True, default=None,
                           verbose_name="URL")
    visits = models.BigIntegerField(null=True, default=0, verbose_name="浏览量")
    users = models.ManyToManyField(User, symmetrical=False,
                                   related_name='enterpises', verbose_name="收藏者")
    # recruits = [Recruit1, Recruit2, Recruit3]
    class Meta:
        db_table = "tb_enterprise"
        verbose_name = "企业"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

"""职位"""
class Recruit(models.Model):

    STATE_CHOICES = (
        (0, "不可用"),
        (1, "可用"),
    )
    jobname = models.CharField(max_length=100, null=True, default=None,
                               verbose_name="职位名称")
    salary = models.CharField(max_length=1000, null=True, default=None,
                              verbose_name="薪资范围")
    condition = models.CharField(max_length=100, null=True, default=None,
                                 verbose_name="经验要求")
    education = models.CharField(max_length=100, null=True, default=None,
                                 verbose_name="学历要求")
    type = models.CharField(max_length=10, null=True, default=None,
                            verbose_name="任职方式")
    city = models.CharField(max_length=100, null=True, default=None,
                            verbose_name="办公所在城市")
    address = models.CharField(max_length=100, null=True, default=None,
                               verbose_name="办公地址")
    state = models.SmallIntegerField(choices=STATE_CHOICES, null=True, default=1, verbose_name="状态")
    labels = models.CharField(max_length=100, null=True, default=None, verbose_name="职位标签", help_text="多个标签以空格隔开")
    detailcontent = RichTextUploadingField(default='', verbose_name='职位描述')
    detailrequire = RichTextUploadingField(default='', verbose_name='职位要求')
    visits = models.BigIntegerField(null=True, default=0, verbose_name="浏览量")
    createtime = models.DateTimeField(null=True, auto_now_add=True, verbose_name="创建日期")
    enterprise = models.ForeignKey(Enterprise, related_name="recruits", on_delete=models.CASCADE, verbose_name="企业ID")
    users = models.ManyToManyField(User, symmetrical=False,
                                   related_name='retruits', verbose_name="收藏者")

    class Meta:
        db_table = "tb_recruit"
        verbose_name = "职位"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.enterprise.name + "_" + self.jobname

"""城市"""
class City(models.Model):

    HOT_CHOICES = ((0, "不是热门"), (1, "是热门"),)
    name = models.CharField(max_length=20, null=True, default=None, verbose_name="城市名称")
    ishot = models.SmallIntegerField(choices=HOT_CHOICES, null=True, default=1, verbose_name="是否热门")

    class Meta:
        db_table = "tb_city"
        verbose_name = "城市"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

